let express = require('express');
let router = express.Router();
let User = require("../model/user.js");

/* GET users listing. */
// /users
router.get('/', function (req, res, next) {
  res.send('respond with a resource');
});

router.get("/isExistUser",function(req,res,next){

})

// /users/reg   注册(post  => {user,pwd,phone,email})
router.post('/reg', function (req, res, next) {
  // 验证参数
  let body = req.body;
  let { user, pwd, phone, email } = body;
  console.log(body);
  if (user && pwd && phone && email) {
    next();
  } else {
    let obj = {
      status: false,
      message: "Fail",
      reason: "请传入完整参数",
    }
    res.json(obj);
  }
})
router.post('/reg', function (req, res, next) {
  let {user} = req.body;
  User.findOne({ user: user }).then((info) => {
    console.log("用户名是否存在", info);
    if (!info) {  // 用户名可以用 => 验证手机号
      next();
    } else {
      let obj = {
        status: false,
        message: "该用户已被注册",
      }
      res.json(obj);
    }

  }).catch(err => {
    let obj = {
      status: false,
      message: "Fail",
      reason: err.message,
    }
    res.json(obj);
  })
})
router.post('/reg', function (req, res, next) {
  let {phone} = req.body;
  User.findOne({ phone: phone }).then((info) => {
    console.log("手机号是否存在", info);
    if (!info) {  // 手机号可以用 => 验证邮箱
      next();
    } else {
      let obj = {
        status: false,
        message: "该手机号已被注册",
      }
      res.json(obj);
    }

  }).catch(err => {
    let obj = {
      status: false,
      message: "Fail",
      reason: err.message,
    }
    res.json(obj);
  })
})
router.post('/reg', function (req, res, next) {
  let {email} = req.body;
  User.findOne({ email: email }).then((info) => {
    console.log("邮箱是否存在", info);
    if (!info) {  // 邮箱可以用 => 注册
      next();
    } else {
      let obj = {
        status: false,
        message: "该邮箱已被注册",
      }
      res.json(obj);
    }

  }).catch(err => {
    let obj = {
      status: false,
      message: "Fail",
      reason: err.message,
    }
    res.json(obj);
  })
})
router.post('/reg', function (req, res, next) {


  // 注册之前先添加 用户名 手机号 邮箱
  // 先验证用户名 -> 手机号 -> 邮箱 -> 注册 => 四层嵌套 
  // 解决方法1: async await 配合try... catch
  // 解决方法2: 在中间件中分步处理

  /* User.findOne({ user: user }).then((info) => {
    console.log("是否存在", info);
    if (!info) {  // 用户名可以用 => 验证手机号
      
    } else {
      let obj = {
        status: false,
        message: "该用户已被注册",
      }
      res.json(obj);
    }

  }).catch(err => {
    let obj = {
      status: false,
      message: "Fail",
      reason: err.message,
    }
    res.json(obj);
  }) */


  // 无脑注册 => 不管用户名 手机号 邮箱是否存在都可以注册 
  User.insertMany(req.body).then((result) => {
    // console.log("注册成功", result);
    let obj = {
      status: true,
      message: "Ok",
    }
    res.json(obj);
  }).catch(err => {
    console.log("注册失败", err);
    let obj = {
      status: false,
      message: "Fail",
      reason: err.message,
    }
    res.json(obj);
  })



});

module.exports = router;
